<template>
  <div class="channelEdit">
    <van-cell center :border="false">
      <div slot="title">我的频道</div>
      <van-button
        class="channel-edit-btn"
        plain
        type="info"
        round
        size="mini"
        @click="delBtn = !delBtn"
        >{{ delBtn ? '完 成' : '编 辑' }}</van-button
      >
    </van-cell>

    <van-grid :column-num="4" :gutter="10">
      <van-grid-item
        class="grif-item-channel"
        :class="{
          active: index === active
        }"
        :icon="delBtn && index !== 0 ? 'clear' : ''"
        v-for="(channel, index) in userChannels"
        :key="index"
        :text="channel.division"
        @click="handleChannel(channel, index)"
      />
    </van-grid>

    <van-cell center :border="false">
      <div slot="title">推荐频道</div>
    </van-cell>

    <van-grid :column-num="4" :gutter="10">
      <van-grid-item
        class="grif-item-channel"
        v-for="(channel, index) in filterChannels"
        :key="index"
        :text="channel.division"
        @click="addChannel(channel)"
      />
    </van-grid>
  </div>
</template>
<script>
import { getChannelsList, addUserChannel, delUserChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setStorage } from '@/utils/storage'
export default {
  name: 'channelEdit',
  data () {
    return {
      channels: [],
      delBtn: false
    }
  },
  created () {
    this.getAllChannels()
  },
  methods: {
    // 编辑频道
    editChannels () {
      console.log('编辑频道')
    },
    // 获取所有频道
    async getAllChannels () {
      try {
        const { data } = await getChannelsList()
        this.channels = data.channels.filter((channel) => {
          return channel.division !== '推荐'
        })
      } catch (error) {
        return error
      }
    },
    // 添加频道
    async addChannel (channel) {
      this.userChannels.push(channel)
      if (this.token) {
        // 存在，有数据，即为登陆
        try {
          await addUserChannel(channel.id)
        } catch (error) {
          return error
        }
      } else {
        // 没登录，存储到本地
        setStorage('userChannels', this.userChannels)
      }
    },
    // 编辑频道
    handleChannel (channel, index) {
      // 删除状态
      if (this.delBtn && index !== 0) {
        this.delChannel(channel, index)
      } else {
        // 编辑
        this.changeChannel(index)
      }
    },
    // 删除频道
    async delChannel (channel, index) {
      if (index <= this.active) {
        this.$emit('changeActive', this.active - 1)
      }
      this.userChannels.splice(index, 1)
      // 判断是否登陆-存储数据
      if (this.token) {
        // 存在，有数据，即为登陆
        try {
          await delUserChannel(channel.id)
        } catch (error) {
          return error
        }
      } else {
        // 没登录，存储到本地
        setStorage('userChannels', this.userChannels)
      }
    },
    // 切换频道
    changeChannel (index) {
      this.$emit('changeChannel', index)
      this.$emit('closeChannelsList')
    }
  },
  components: {},
  props: {
    userChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  watch: {},
  computed: {
    filterChannels () {
      return this.channels.filter((channel) => {
        return !this.userChannels.find((userChannel) => {
          return userChannel.id === channel.id
        })
      })
    },
    ...mapState({
      token: 'token'
    })
  },
  mounted () {}
}
</script>

<style lang="less" scoped>
.channelEdit {
  padding: 54px 10px 10px;
  .channel-edit-btn {
    width: 60px;
  }
  .grif-item-channel {
    position: relative;
    height: 43px;
    /deep/.van-grid-item__content {
      background-color: #ebedf0;
      .van-grid-item__text {
        margin-top: 0;
        font-size: 13px;
      }
    }
    /deep/ .van-grid-item__icon {
      position: absolute;
      right: -5px;
      top: -5px;
      color: #ccc;
      font-size: 18px;
    }
  }
  .active {
    /deep/ .van-grid-item__text {
      color: #1989fa;
    }
  }
}
</style>
